package 第五周;

/**
 * @author xhw
 */

public class 删除有序数组中的重复项 {

    public static void main(String[] args) {
        int[] nums = {1, 1, 2}; // 示例数组
        int newLength = removeDuplicates(nums);

        // 打印数组新长度和更新后的数组内容
        System.out.println("新数组长度：" + newLength);
        for (int i = 0; i < newLength; i++) {
            System.out.print(nums[i] + " ");
        }
    }

    public static int removeDuplicates(int[] nums) {
        if (nums.length == 0) return 0;
        int i = 0;
        for (int j = 1; j < nums.length; j++) {
            if (nums[j] != nums[i]) {
                i++;
                nums[i] = nums[j];
            }
        }
        return i + 1;
    }
}